﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace ZHDJ.Data.Models.Common
{
    /// <summary>
    /// 历史积分
    /// </summary>
   public class PartyObjectScoreHistory : EntityBase
    {
        #region 属性
        /// <summary>
        /// 原积分ID
        /// </summary>
        public string ScoreID { get; set; }
        /// <summary>
        /// 获得积分的人/组织ID
        /// </summary>
        public string ObjectID { get; set; }

        /// <summary>
        /// Member人员，Branch组织
        /// </summary>
        public string ObjectType { get; set; }

        /// <summary>
        /// 获得时间
        /// </summary>
        public DateTime? ScoreDate { get; set; }

        /// <summary>
        /// 积分类型
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 积分
        /// </summary>
        public decimal Score { get; set; }

        /// <summary>
        /// 积分来源（保留字段）
        /// </summary>
        public string SourceType { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 关联ID(关联产生积分的业务ID)
        /// </summary>
        public string ForeignID { get; set; }

        /// <summary>
        /// 扩展标识
        /// </summary>
        public string Flag { get; set; }

        /// <summary>
        /// 积分所属方式（标识）
        /// </summary>
        public string ScoreType { get; set; }

        /// <summary>
        /// 积分描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 转为历史积分时间
        /// </summary>
        public string BakTime { get; set; }

        /// <summary>
        /// 积分所属年份
        /// </summary>
        public string ScoreYear { get; set; }

        #endregion


        /// <summary>
        /// 个人积分 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="category"></param>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        public static DataSet GetGridDataMember(int pageIndex, int pageSize, string category, string searchKey = "", string scoreYear = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  data.*,m.UserName name,m2.UserName CreateByName,r.Name CategoryName,
                        case data.ObjectType when 'Branch' then '组织积分' else '个人积分' end as  ObjectTypeName
                        from tblPartyObjectScoreHistory data
                        inner join tblpartyorgmember m on(m.LoginID= data.ObjectID)
                        inner join tblpartyorgmember m2 on(m2.LoginID= data.CreateBy)
                        inner join tblPartyObjectScoreRule r on(r.objectType='Member' and r.Category=data.Category)
                        where data.objectType='Member'  ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and m.UserName like ?key ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "key", Value = string.Format("%{0}%", searchKey) });
            }

            if (!string.IsNullOrEmpty(category))
            {
                sb.Append(" and data.Category=?Category");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "Category", Value = category });
            }
            if (!string.IsNullOrEmpty(scoreYear))
            {
                sb.Append(" and data.scoreyear=?scoreyear");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "scoreyear", Value = scoreYear });
            }
            sb.Append(" order by data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        /// <summary>
        /// 组织积分
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="category"></param>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        public static DataSet GetGridDataBranch(int pageIndex, int pageSize, string category, string searchKey = "",string scoreyear="")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  data.*,b.BranchName as Name,m2.UserName CreateByName,r.Name CategoryName,
                        case data.ObjectType when 'Branch' then '组织积分' else '个人积分' end as  ObjectTypeName
                        from tblPartyObjectScoreHistory data
                        inner join tblpartyorgbranch b on (b.branchid=data.objectid) 
                        inner join tblpartyorgmember m2 on(m2.LoginID= data.CreateBy)
                        inner join tblPartyObjectScoreRule r on(r.objectType='Branch' and r.Category=data.Category)
                        where   data.objectType='Branch' ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and b.BranchName like ?key ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "key", Value = string.Format("%{0}%", searchKey) });
            }

            if (!string.IsNullOrEmpty(category))
            {
                sb.Append(" and data.Category=?Category");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "Category", Value = category });
            }
            if (!string.IsNullOrEmpty(scoreyear))
            {
                sb.Append(" and data.scoreyear=?scoreyear");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "scoreyear", Value = scoreyear });
            }
            sb.Append(" order by data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
    }
}
